//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.bpm.engine.action.handler.task;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.act.service.ActTaskService;
import com.dstz.bpm.api.constant.EventType;
import com.dstz.bpm.api.constant.NodeType;
import com.dstz.bpm.api.engine.plugin.cmd.TaskCommand;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.inst.IBpmInstance;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.core.manager.BpmTaskManager;
import com.dstz.bpm.core.model.BpmInstance;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.bpm.engine.action.handler.AbsActionHandler;
import javax.annotation.Resource;

public abstract class AbstractTaskActionHandler<T extends DefualtTaskActionCmd> extends AbsActionHandler<T> {
   @Resource
   protected ActTaskService aI;
   @Resource
   protected BpmTaskManager aJ;
   @Resource
   protected TaskCommand aK;

   public AbstractTaskActionHandler() {
   }

   // 1
   @Override
   protected void b(T actionModel) {
      BpmTask bpmTask = (BpmTask)actionModel.getBpmTask();
      String taskId = bpmTask.getTaskId();
      String destinationNode = bpmTask.getBackNode();
      if (StringUtil.isEmpty(destinationNode)) {
         destinationNode = actionModel.getDestination();
      }

      if (StringUtil.isEmpty(destinationNode)) {
         this.aI.completeTask(taskId, actionModel.getActionVariables());
      } else {
         this.aI.completeTask(taskId, actionModel.getActionVariables(), new String[]{destinationNode});
      }

   }
   // 1
   @Override
   protected void c(T data) {
      if (data.getBpmTask() == null) {
         BpmTask task = (BpmTask)this.aJ.get(data.getTaskId());
         if (task == null) {
            throw new BusinessException(BpmStatusCode.TASK_NOT_FOUND);
         } else {
            data.setBpmTask(task);
            data.setDefId(task.getDefId());
            data.setBpmInstance((IBpmInstance)this.f.get(task.getInstId()));
            this.m(data);
            this.a(data, this.a.getBpmNodeDef(task.getDefId(), task.getNodeId()));
         }
      }
   }

   @Override
   public Boolean isSupport(BpmNodeDef nodeDef) {
      NodeType nodeType = nodeDef.getType();
      return nodeType != NodeType.USERTASK && nodeType != NodeType.SIGNTASK ? false : true;
   }

   // 1
   @Override
   public void h(DefualtTaskActionCmd actionModel) {
      this.aK.execute(EventType.TASK_PRE_COMPLETE_EVENT, actionModel);
   }

   // 1
   @Override
   protected void i(T actionModel) {
      this.h(actionModel);
      if (actionModel.isSource()) {
         BpmInstance instance = (BpmInstance)actionModel.getBpmInstance();
         if (instance.a()) {
            this.f.update(instance);
         }
      }

   }

   @Override
   public String getDefaultGroovyScript() {
      return "";
   }
}
